package com.example.service.impl;

import com.example.mapper.EmpMapper;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
  //分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender,
                         LocalDate begin, LocalDate end) {

       /* //1,获取总记录数
        Long count = empMapper.count();

        //2，获取分页查询结果列表
        Integer start = (page - 1) * pageSize;//计算每页的起始索引
        List<Emp> empList =  empMapper.page(start,pageSize);

        //3，封装pagebean对象
        PageBean pageBean = new PageBean(count,empList);

        return pageBean;*/

        //1,设置分页参数
        PageHelper.startPage(page,pageSize);

        //2，执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p  = (Page<Emp>) empList;

        //3，封装pagebean对象
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    //批量删除
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }


    //新增员工
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        //调用empmapper中的方法进行插入操作
        empMapper.insert(emp);
    }

    //根据id查询员工
    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }


    //更新员工
    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }


    //员工登录
    @Override
    public Emp login(Emp emp) {
       return empMapper.getUsernameAndPassword(emp);
    }
}
